package GUIHelper;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ControlLayer.RoomTypeCtr;
import GUILayer.RoomTypeGUI;
import ModelLayer.KeyWord;

public class RoomTypeInfoPanel extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;
	private JPanel roomTypeGUI;
	private JLabel lblname;
	private JLabel lblprice;
	private JLabel lblsize;
	// private JTextField textField_name;
	private JTextField textField_price;
	private JTextField textField_size;
	private JButton btnSave;
	private JButton btnCancel;
	private JPanel infoPanel;
	private JPanel btnPanel;
	private JComboBox<String> roomTypes;
	private RoomTypeCtr rtCtr;
	private String[] listStrings = { KeyWord.SingleRoom.toString(),
			KeyWord.DoubleRoom.toString(), KeyWord.FamilyRoom.toString() };

	public RoomTypeInfoPanel(JPanel roomTypeGUI) {
		rtCtr = new RoomTypeCtr();
		this.roomTypeGUI = roomTypeGUI;
		initialize();

	}

	public void initialize() {

		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		infoPanel = new JPanel();
		btnPanel = new JPanel();
		this.add(infoPanel);
		this.add(btnPanel);

		btnSave = new JButton("Save");
		btnCancel = new JButton("Cancel");
		btnPanel.setLayout(new FlowLayout());
		btnPanel.add(btnSave);
		btnPanel.add(btnCancel);

		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				disposeParent();
			}
		});

		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createRoomType();
				((RoomTypeGUI) roomTypeGUI).initialize();
			}
		});

		lblname = new JLabel("Name: ");
		lblprice = new JLabel("Price Per Night: ");
		lblsize = new JLabel("Size: ");
		// textField_name = new JTextField();
		textField_price = new JTextField();
		textField_size = new JTextField();

		roomTypes = new JComboBox<String>(listStrings);
		roomTypes.setSelectedIndex(2);
		roomTypes.addActionListener(this);

		GroupLayout gl = new GroupLayout(infoPanel);
		infoPanel.setLayout(gl);
		infoPanel.setBorder(BorderFactory.createTitledBorder("RoomType Info"));
		gl.setAutoCreateGaps(true);
		gl.setAutoCreateContainerGaps(true);

		gl.setHorizontalGroup(gl
				.createSequentialGroup()
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addComponent(lblname).addComponent(lblprice)
								.addComponent(lblsize))
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addComponent(roomTypes)
								.addComponent(textField_price)
								.addComponent(textField_size)));
		gl.setVerticalGroup(gl
				.createSequentialGroup()
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(lblname).addComponent(roomTypes))
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(lblprice)
								.addComponent(textField_price))
				.addGroup(
						gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(lblsize)
								.addComponent(textField_size)));
	}

	public void disposeParent() {
		((JFrame) this.getParent().getParent().getParent().getParent())
				.dispose();
	}

	public void createRoomType() {
		String roomType = listStrings[roomTypes.getSelectedIndex()];
		try {
			rtCtr.insertRoomType(roomType,
					Double.parseDouble(textField_price.getText()),
					Integer.parseInt(textField_size.getText()));
			disposeParent();
			JOptionPane.showMessageDialog(null, "The room type is created.",
					"Create Room Type", JOptionPane.INFORMATION_MESSAGE);

		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Please input correct value",
					"Error", JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// JComboBox cb = (JComboBox) e.getSource();
		// String list = (String) cb.getSelectedItem();

	}
}
